#include "question4.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <QDialog>
#include <QLineEdit>
#include <QRegExpValidator>

Question4::Question4(QWidget *parent) : QMainWindow(parent)
{
    this->setFixedSize(1200,800);

    this->setWindowIcon(QPixmap(":/pictures/title.png"));

    this->setWindowTitle("问题");

    MyPushButton * backBtn = new MyPushButton(":/pictures/backbutton.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    MyPushButton * checkBtn = new MyPushButton(":/pictures/answer.png");
    checkBtn->setParent(this);
    checkBtn->move(800,600);

    w = new Win;

    connect(checkBtn,&MyPushButton::clicked,[=](){

        checkBtn->zoom1();
        checkBtn->zoom2();

        QTimer::singleShot(500,this,[=](){
            QDialog dialog; dialog.setWindowTitle("输入密码");
            dialog.resize(300, 150);
            QPushButton yesBT("确定", &dialog);
            yesBT.setGeometry(10, 75, 100, 35);
            QPushButton noBT("取消", &dialog);
            noBT.setGeometry(190, 75, 100, 35);
            QLineEdit input("", &dialog);
            input.setGeometry(50, 30, 200, 25);
            input.setEchoMode(QLineEdit::PasswordEchoOnEdit);

            QRegExp regExp("^[0-9]*[1-9][0-9]*$");
            QRegExpValidator *pattern = new QRegExpValidator(regExp, this);
            input.setValidator(pattern);
            connect(&yesBT, &QPushButton::clicked, &dialog, &QDialog::accept);
            connect(&noBT, &QPushButton::clicked, &dialog, &QDialog::reject);
            int ret = dialog.exec();
            if (ret == QDialog::Accepted)
            {
                if (input.text().toInt() == 4502)
                {
                    this->hide();
                    w->show();
                }
            }
            if (ret == QDialog::Rejected)
            {
                dialog.close();
            }
    });
});

    connect(backBtn,&MyPushButton::clicked,[=](){
        backBtn->zoom1();
        backBtn->zoom2();

        QTimer::singleShot(500,this,[=](){
            emit this->q4b();
        });
    });
}

void Question4::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/pictures/mi4.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}
